package cn.icare.cms.controller.restful;

import cn.icare.cms.PromotionService;
import cn.icare.cms.model.ro.AddPromotionRO;
import cn.icare.cms.model.ro.EditPromotionRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/promotion")
@Api(tags = "REST促销管理")
public class PromotionController extends BaseController {
    @Resource
    private PromotionService promotionService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/promotion
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "新增单个促销", notes = "t_promotion表新增单个促销")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddPromotionRO ro, HttpServletRequest request) {

        //检查名称是否重复
        boolean isExists = this.promotionService.exists(0, ro.getTitle());
        if (isExists) {

            return super.getRespFail("促销名称已经存在，不能重复添加");
        }
        String systemUserName = getCurrentUserName(request);
        Integer id = this.promotionService.createOne(ro, systemUserName);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/promotion
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个促销", notes = "t_promotion表更新单个促销")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditPromotionRO ro, HttpServletRequest request) {
        //检查名称是否重复
        boolean isExists = this.promotionService.exists(ro.getId(), ro.getTitle());
        if (isExists) {
            return super.getRespFail("促销名称已经存在，不能重复添加");
        }
        String systemUserName = getCurrentUserName(request);
        if (this.promotionService.updateOne(ro, systemUserName)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/promotion/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个促销", notes = "t_promotion表删除符合主键的单个促销")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Integer id) {
        if (this.promotionService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/promotion?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个促销", notes = "t_promotion表删除符合主键的多个促销")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Integer> ids) {
        if (this.promotionService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/promotion/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个促销", notes = "t_promotion表返回符合主键的单个促销")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Integer id) {
        return super.getRespOk(this.promotionService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/promotion?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个促销", notes = "返回符合条件的多个促销")
    public ResponseEntity<RespMsgVO> getList(
            @RequestParam(name = "kind", required = false) Integer kind,
            @RequestParam(name = "type", required = false) Integer type,
            @RequestParam(name = "title", required = false) String title,
            @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize

    ) {
        return super.getRespOk(this.promotionService.getList(kind, type, title, pageNum, pageSize));
    }


    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddPromotionRO ro, HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditPromotionRO ro, HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Integer> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(Integer kind, Integer type, String title, Integer pageNum, Integer pageSize) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
